package jcl;

/**
 * Robocode - Competitive Robot
 * ICS 413 Fall 2010
 */

import java.awt.Color;
import java.util.Random;
import robocode.BulletHitEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.WinEvent;

/**
 * MajorTom - A competitive robot designed for my ICS 413 class.
 * 
 * Moves about the battlefield randomly using Random(). Scans for any bot and fires upon it.
 * 
 * @author Jerry Lau
 */
public class MajorTom extends Robot {
  String possibleTarget;
  int possibleTargetCounter = 0;

  /**
   * Run method for robot.
   */
  public void run() {
    setBodyColor(Color.orange);
    setGunColor(Color.orange);
    setRadarColor(Color.orange);
    setScanColor(Color.orange);

    // Variables for random calculation
    Random rng = new Random();
    int randomAngle;
    int temp;
    int randomDistance;

    // Random movement
    while (true) {
      turnGunRight(360);
      // Generates a random direction for the robot to travel from 1 to 90 degrees
      randomAngle = rng.nextInt(90) + 1;
      // Decides whether the angel will be positive or negative
      temp = rng.nextInt(2) + 1;
      // Makes angle negative
      if (temp == 2) {
        randomAngle = -randomAngle;
      }
      // Adjust robot to face new random direction
      turnRight(randomAngle);
      // Generates a random distance to travel from 75 - 200 pixels
      randomDistance = rng.nextInt(126) + 75;

      // Random heading
      temp = rng.nextInt(2) + 1;
      if (temp == 1) {
        ahead(randomDistance);
      }
      else {
        back(randomDistance);
      }

    } // end while
  } // end run

  /**
   * onScannedRobot: Fire bullets proportional to enemy's distance.
   * 
   * @param event - Information on the enemy robot
   */
  public void onScannedRobot(ScannedRobotEvent event) {

    // Do nothing if energy is below 11 but there is more than one robot
    // alive (evades until one
    // remains)
    if (!(this.getOthers() > 1) && (this.getEnergy() < 11)) {
      // Check for an easy target
      if (possibleTargetCounter >= 2) {
        if (event.getName().equals(possibleTarget)) {

          if (possibleTargetCounter >= 3) {
            fire(3);
            possibleTargetCounter--;
          }
          else {
            fire(1);
            possibleTargetCounter--;
          }

        }
      }

      else {
        possibleTargetCounter--;
        // Use adjusted fire power when only one enemy remains AND it
        // has more energy
        if ((this.getOthers() == 1) && (event.getEnergy() > this.getEnergy())) {
          if (event.getDistance() < 50) {
            fire(3);
          }
          else {
            fire(.5);
          }
        }
        // Otherwise adjust bullet proportion based on distance
        else {
          if (event.getDistance() < 150) {
            fire(3);
          }
          else if (event.getDistance() < 30) {
            fire(2);
          }
          else if (event.getDistance() < 600) {
            fire(1.1);
          }
          else {
            fire(.5);
          }
        }
      }

    } // end else (out-most)
  } // end onScannedRobot

  /**
   * onBulletHit: Used to determine if there is an easy target base on successful bullet hits.
   * 
   * @param directHit - Condition when a bullet hits a target.
   */
  public void onBulletHit(BulletHitEvent directHit) {
    possibleTarget = directHit.getName();
    possibleTargetCounter = +1;
  }

  /**
   * onWin: Perform a victory dance.
   * 
   * @param win - Upon achieving victory, perform this action
   */
  public void onWin(WinEvent win) {
    while (true) {
      turnRight(20);
      turnLeft(20);
    }
  } // end onWin
} // end MajorTom
